package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserLevelInfoDao;
import com.frogs.ucenter.entity.UserLevelInfo;
import com.frogs.ucenter.entity.UserPointLog;
import com.frogs.ucenter.v1.request.UserLevelInfoQueryRequest;
import com.frogs.ucenter.v1.request.UserLevelInfoSaveRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.ShowResponse;
import com.frogs.ucenter.v1.service.UserLevelInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION + "userLevelInfoService")
public class UserLevelInfoServiceImpl implements UserLevelInfoService {
	
	private static final Logger log = LoggerFactory.getLogger(UserLevelInfoServiceImpl.class);
	
	@Resource
    private UserLevelInfoDao userLevelInfoDao;

    /**
     * 判断id是否已经存在表中，如果不存在，执行插入操作，否则执行更新操作
     * @author hongyuhao
     */
	@Override
	public Response mergeLevelInfo(Request request) throws Exception  {
		UserLevelInfoSaveRequest ulisr = (UserLevelInfoSaveRequest)request;
		UserLevelInfo ulf = ulisr.getUserLevelInfo();
		UserPointLog upl = ulisr.getUserPointLog();
		try {
			Response response;
			
			if(null != userLevelInfoDao.selectById(ulf.getId())) {
				userLevelInfoDao.update(ulf);
			} else {
				userLevelInfoDao.insert(ulf);
			}
			response = new AddResponse<UserLevelInfo>();
			upl.setUpResultCode(ApiResultEnum.SUCCESS.getId());
			return response;
		} catch(Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}
	
	@Override
	public Response getLevelInfo(Request request) throws Exception  {
		try {
			UserLevelInfoQueryRequest uliqr = (UserLevelInfoQueryRequest)request;
			ShowResponse<UserLevelInfo> showResponse = new ShowResponse<UserLevelInfo>();
			int id = uliqr.getId();
			showResponse.setInfo(userLevelInfoDao.selectById(id));
			return showResponse;
		} catch(Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}
}